<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_parse extends base_model{


	//hook mod_parse_start.php
	function __construct() {
	
		//hook mod_parse_init_init_before.php
		parent::__construct();
		//hook mod_parse_init_init_end.php
	
	}
	
	public function parse_facename() {
		//hook mod_parse_parse_facename.php
		return array(	'开心','惊讶','咬牙','汗水','大哭',
							'伤心','卖萌','懂了','吓尿','赞');
	}
	
	public function parse_face(&$content) {
		//hook mod_parse_parse_face.php
		$content = preg_replace('/{JYFACE:([1-9]|[1][0])}/', '<img src="jy_src/face/\\1.png">', $content);
	}
	
	public function parse_content(&$content, $cut = true) {
		//hook mod_parse_parse_content.php
		$content = preg_replace('/([\n\r]{2,})/',"\n",$content);
		$content = $content == "\n" ? '' : $content;
		if($cut) $content = misc::utf8_sub_str($content,0,500);
	}
	
	public function parse_title(&$title) {
		//hook mod_parse_parse_title.php
		$title = preg_replace('/([\s]+)/','',$title);
		$title = misc::utf8_sub_str($title,0,200);
	}
	
	public function parse_thumb_name($path) {
		//hook mod_parse_parse_thumb_name.php
		$path = preg_replace('/\w+\/\d+\/([a-zA-Z0-9]{32,})\.\w+/','\\1',$path);
		return $path;
	}

	public function parse_get_at_users_name($str) {
		//hook mod_parse_parse_get_at_users_name.php
		$ret = $arr = array();
		preg_match_all('/@([\w\x{4e00}-\x{9fa5}]{1,20}) /u',$str,$arr);
		$ret = (count($arr[1]) > 20) ? array_slice($arr[1],0,20) : $arr[1];
		return array_unique($ret);
	}

	public function parse_all_image_md5($content) {
		//hook mod_parse_parse_all_image_md5.php
		$ret = $arr = array();
		preg_match_all('/{JYIMGS:([a-zA-Z0-9]{32,})}/', $content,$arr);
		$ret = (count($arr[1]) > 50) ? array_slice($arr[1],0,50) : $arr[1];
		return array_unique($ret);
	}
	
	public function parse_all_files_md5($content) {
		//hook mod_parse_parse_all_files_md5.php
		$ret = $arr = array();
		preg_match_all('/{JYFILE:([a-zA-Z0-9]{32,})}/', $content,$arr);
		$ret = (count($arr[1]) > 50) ? array_slice($arr[1],0,50) : $arr[1];
		return array_unique($ret);
	}
	
	public function parse_all_image_in_content(&$content) {
		//hook mod_parse_parse_all_image_in_content.php
		$ret = $arr = array();
		$md5u = $this->parse_all_image_md5($content);
		foreach($md5u as $k=>$v) {
			$tmp = $this->db->get("attach-md5-$v");
			if(!empty($tmp)) {
				$thmbpath = str_replace(CONFIG_UPS_PATH, CONFIG_THU_PATH, $tmp['path']);
				if(!file_exists(IN_APP.$thmbpath)) {
					$thmbpath = $tmp['path'];
				}
				//hook mod_parse_parse_all_image_in_content_in_foreach.php
				$infos = core::json_decode($tmp['infos']);
				$fname = $infos == false ? '' : '_'.$infos['fname'];
				$fsize = $infos == false ? '' : '(大小：'.misc::humansize($infos['fsize']).')';
				
				$content = str_replace('{JYIMGS:'.$v.'}',
					'<br/><img src="'.$thmbpath.'"/>
					 <br/><a href="index.php?attach-load-md5-'.$v.'.htm">[查看原图'.$fname.']'.$fsize.'</a>
					 <br/>' ,$content);
			}
		}
	}
	
	public function parse_all_files_in_content(&$content) {
		//hook mod_parse_parse_all_files_in_content.php
		$ret = $arr = array();
		$md5u = $this->parse_all_files_md5($content);
		foreach($md5u as $k=>$v) {
			$tmp = $this->db->get("attach-md5-$v");
			if(!empty($tmp)) {
				$infos = core::json_decode($tmp['infos']);
				$fname = $infos == false ? '' : '_'.$infos['fname'];
				$fsize = $infos == false ? '' : '(大小：'.misc::humansize($infos['fsize']).')';
				
				//hook mod_parse_parse_all_files_in_content_in_foreach.php
				$content = str_replace('{JYFILE:'.$v.'}',
					'<br/>附件：<a href="index.php?attach-load-md5-'.$v.'.htm">[下载文件'.$fname.']'.$fsize.'</a>
					 <br/>' ,$content);
			}
		}
	}
	
	//hook mod_parse_end.php
}
/*{TranslateTagEnd}*/

?>